﻿////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2008 Chris Colinsky
// All Rights Reserved
//
// version	0.4
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

import sugarcookie.events.EventController;
import sugarcookie.video.FLVController;

import mx.utils.Delegate;

/**
 * Class is a wrapper for NetStream
 */
class sugarcookie.video.FLVLoader extends EventController {
	private var controller:FLVController;
	private var display:Video;
	private var sound:Sound;
	private var muted:Boolean = false;
	private var nc:NetConnection;
	private var ns:NetStream;
	private var vData:Array = [];
	private var duration:Number;
	
	/**
	 * Constructor - sets properties to default values and makes argument assignments
	 * 
	 */
	
	public function FLVLoader(v:Video, c:FLVController){
		super(this);
		_log.debug("instantiating FLVLoader");
		//assigns the Video object to display var, instantiates the NetConnection and NetStream objects, and assigns the FLVController object
		controller = c;
		display = v;
		//adds event listeners to the controller instance
		controller.addEventListener("playStream", Delegate.create(this,onPlay));
		controller.addEventListener("pauseStream", Delegate.create(this,onPause));
		controller.addEventListener("stopStream", Delegate.create(this,onStop));
		controller.addEventListener("seekStart", Delegate.create(this,seekStart));
		controller.addEventListener("toggleSound", Delegate.create(this,toggleSound));
		controller.addEventListener("enterFrame", Delegate.create(this,enterFrame));
		init();
	}
	
	private function init():Void{
		var rObj:Object = this;
		nc = new NetConnection();
		nc.connect(null);
		ns = new NetStream(nc);
		display.attachVideo(ns);
		ns.onMetaData = function(myMeta):Void { 
			 for (var i in myMeta) { 
				  //rObj._log.debug(i + ":\t" + myMeta[i]) 
			 }
			 rObj.duration = myMeta.duration;
			 vData = myMeta;
		}
		ns.onStatus = Delegate.create(this, onStatus);
		sound = new Sound();
		sound.setVolume(100);
	}
	
	public function loadVideo(url:String, buffer:Number):Void{
		ns.setBufferTime(buffer);
		ns.play(url);
	}
	
	private function onStatus(evt:Object) {
		var str:String = String(evt.code);
		postEvent("updateStatus", str, true, true, {});
		for (var prop in evt) {
            trace("\t"+prop+":\t"+evt[prop]);
        }
	}
	
	private function toggleSound(evt:Object):Void{
		_log.debug("toggleSound from FLVLoader with sound: "+sound);
		if(muted==true){
			_log.debug("muted = true and sound: "+sound);
			sound.setVolume(100);
			muted = false;
		}else{
			_log.debug("muted = false and sound: "+sound);
			sound.setVolume(0);
			muted = true;
		}
	}
	
	private function onPlay(evt:Object):Void{
		//only "unpauses" if the stream is already paused...if the stream is playing, this does nothing.
		ns.pause(false);
	}
	
	private function onPause(evt:Object):Void{
		//toggles the "pause" state
		ns.pause();
	}
	
	private function onStop(evt:Object):Void{
		//only pauses if the stream is playing...seeks to 0 regardless
		ns.pause(true);
		ns.seek(0);
	}
	
	private function seekStart(evt:Object):Void{
		ns.seek(0);
	}
	
	/*
	 *enterFrame function:
	 *handles updating the scrub-bar, loading-bar, and progress-bar for the controller
	 */
	
	private function enterFrame(evt:Object):Void{
		evt.data._loadingBar._xscale = (ns.bytesLoaded/ns.bytesTotal)*100;
		if(evt.data._dragging){
			var distance:Number = evt.data._playhead._x-evt.data._progressBar._x;
			evt.data._progressBar._width = distance;
			var newPercentPlayed:Number = distance/evt.data._totalWidth;
			var newPlayheadTime:Number = duration*newPercentPlayed;
			//_log.debug("newPercentPlayed: "+newPercentPlayed+ "\n newPlayheadTime: "+newPlayheadTime);
			ns.seek(newPlayheadTime);
		}else{
			var percentPlayed = (ns.time/duration);
			var newWidth:Number = Math.round(percentPlayed*evt.data._totalWidth);
			var newX:Number = evt.data._progressBar._x + newWidth - evt.data._playhead._width;
			evt.data._progressBar._width = newWidth;
			if(newX<evt.data._progressBar._x){
				evt.data._playhead._x = evt.data._progressBar._x;
			}else{
				evt.data._playhead._x = newX;
			}
		}
	}
	
}